package cn.afactory.tygz.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SysMessageItem;
import cn.afactory.tygz.ui.base.utils.DateUtil;


public class SysMessageAdapter extends BaseAdapter {
    private List<SysMessageItem> mList = new ArrayList<SysMessageItem>();
    private Context mContext;

    public SysMessageAdapter(Context ctx, List<SysMessageItem> list) {
        mList = list;
        mContext = ctx;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final SysMessageItem sysMessageItem = mList.get(position);
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sys_message_list,null);

            holder.tv_sys_message_desc = (TextView)convertView.findViewById(R.id.tv_sys_message_desc);
            holder.tv_sys_message_date = (TextView)convertView.findViewById(R.id.tv_sys_message_date);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        if(sysMessageItem.msg != null){
            holder.tv_sys_message_desc.setText(sysMessageItem.msg);
        }

        holder.tv_sys_message_date.setText(DateUtil.getTimeDescription(new Date(sysMessageItem.addTime)));

        return convertView;
    }

    public void setData(List<SysMessageItem> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    static class ViewHolder{
        public TextView tv_sys_message_desc;
        public TextView tv_sys_message_date;
    }
}